﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using saigonbook.Biz;

namespace quanlybansach.Frm
{
    public partial class Frm_xemphieunhap : Form
    {
        public Frm_xemphieunhap()
        {
            InitializeComponent();
            danhsachphieunhap.Columns.Add("Col1", "STT");
            danhsachphieunhap.Columns.Add("Col2", "Mã phiếu");
            danhsachphieunhap.Columns.Add("Col3", "Ngày nhập");
            danhsachphieunhap.Columns.Add("Col4", "Nhà cung cấp");
            danhsachphieunhap.Columns.Add("Col5", "Địa chỉ");
            danhsachphieunhap.Columns.Add("Col6", "SĐT");
            danhsachphieunhap.Columns.Add("Col7", "Email");
            danhsachphieunhap.Columns.Add("Col8", "Tổng tiền (VNĐ)");            
            danhsachphieunhap.Columns[0].Width = 50;
            danhsachphieunhap.Columns[1].Width = 90;
            danhsachphieunhap.Columns[2].Width = 100;
            danhsachphieunhap.Columns[3].Width = 170;
            danhsachphieunhap.Columns[4].Width = 190;
            danhsachphieunhap.Columns[5].Width = 100;
            danhsachphieunhap.Columns[6].Width = 160;
            danhsachphieunhap.Columns[7].Width = 140;
            danhsachphieunhap.AllowUserToAddRows = false;

            danhsachchitietphieunhap.Columns.Add("Col1", "STT");
            danhsachchitietphieunhap.Columns.Add("Col2", "Mã sách");
            danhsachchitietphieunhap.Columns.Add("Col3", "Tên sách");
            danhsachchitietphieunhap.Columns.Add("Col4", "Giá (VNĐ)");
            danhsachchitietphieunhap.Columns.Add("Col5", "Số lượng");
            danhsachchitietphieunhap.Columns.Add("Col6", "Thành tiền (VNĐ)");
            danhsachchitietphieunhap.Columns[0].Width = 40;
            danhsachchitietphieunhap.Columns[1].Width = 78;
            danhsachchitietphieunhap.Columns[2].Width = 160;
            danhsachchitietphieunhap.Columns[3].Width = 100;
            danhsachchitietphieunhap.Columns[4].Width = 100;
            danhsachchitietphieunhap.Columns[5].Width = 140;
            danhsachchitietphieunhap.AllowUserToAddRows = false;
        }

        private List<BIZ_phieunhap> dsphieunhap = new List<BIZ_phieunhap>();

        private void Frm_xemphieunhap_Load(object sender, EventArgs e)
        {
            dsphieunhap.Clear();
            dsphieunhap = BIZ_phieunhap.Select_phieunhap();
            int stt = 0;
            foreach (BIZ_phieunhap _pn in dsphieunhap)
            {
                stt++;
                string tongtien = _pn.tongtien.ToString("c");
                tongtien = tongtien.Substring(1, tongtien.LastIndexOf(".") - 1);
                danhsachphieunhap.Rows.Add(stt, _pn.maphieunhap, _pn.ngaynhap.Date.ToShortDateString(), 
                                            _pn.nhacungcap, _pn.diachi, _pn.sodienthoai, _pn.email, tongtien);
            }
        }

        private void btndong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            Frm_nhaphang f = new Frm_nhaphang();
            f.Visible = true;
        }

        private void chonphieunhap(object sender, DataGridViewCellEventArgs e)
        {
            danhsachchitietphieunhap.Rows.Clear() ;
            if (danhsachphieunhap.CurrentCell != null)
            {
                int i = danhsachphieunhap.CurrentCell.RowIndex;
                txtmaphieu.Text = danhsachphieunhap.CurrentRow.Cells[1].Value.ToString().Trim();
                txtngaynhap.Text = danhsachphieunhap.CurrentRow.Cells[2].Value.ToString().Trim();
                txtnhacungcap.Text = danhsachphieunhap.CurrentRow.Cells[3].Value.ToString().Trim();
                txtdiachi.Text = danhsachphieunhap.CurrentRow.Cells[4].Value.ToString().Trim();
                txtsodienthoai.Text = danhsachphieunhap.CurrentRow.Cells[5].Value.ToString().Trim();
                txtemail.Text = danhsachphieunhap.CurrentRow.Cells[6].Value.ToString().Trim();
                txttongtien.Text = danhsachphieunhap.CurrentRow.Cells[7].Value.ToString().Trim() + " VNĐ";
                List<BIZ_chitietphieunhap> ds = new List<BIZ_chitietphieunhap>();
                BIZ_phieunhap pn = new BIZ_phieunhap();
                pn.maphieunhap = int.Parse(txtmaphieu.Text);
                ds = pn.Chitietphieunhap;
                //ds = pn.Select_chitietphieunhap(int.Parse(txtmaphieu.Text));
                int stt = 0;
                BIZ_sach s = new BIZ_sach();
                foreach(BIZ_chitietphieunhap ct in ds)
                {
                    stt++;
                    string gia = ct.gia.ToString("c");
                    gia = gia.Substring(1, gia.LastIndexOf(".") - 1);
                    string thanhtien = (ct.soluong * ct.gia).ToString("c");
                    thanhtien = thanhtien.Substring(1, thanhtien.LastIndexOf(".") - 1);
                    danhsachchitietphieunhap.Rows.Add(stt, ct.sach.masach, ct.sach.tensach, gia, ct.soluong, thanhtien);
                }
            }
        }

        private void txttimkiem_KeyPress(object sender, KeyPressEventArgs e)
        {
            danhsachphieunhap.Rows.Clear();
            List<BIZ_phieunhap> ds = dsphieunhap;
            ds = (from d in ds where d.nhacungcap.ToLower().Contains(txttimkiem.Text.ToLower()) select d).ToList();
            int stt = 0;
            foreach (BIZ_phieunhap _pn in ds)
            {
                stt++;
                string tongtien = _pn.tongtien.ToString("c");
                tongtien = tongtien.Substring(1, tongtien.LastIndexOf(".") - 1);
                danhsachphieunhap.Rows.Add(stt, _pn.maphieunhap, _pn.ngaynhap.Date.ToShortDateString(),
                                            _pn.nhacungcap, _pn.diachi, _pn.sodienthoai, _pn.email, tongtien);
            }
        }

        private void btntim_Click(object sender, EventArgs e)
        {
            danhsachphieunhap.Rows.Clear();
            BIZ_phieunhap pn = new BIZ_phieunhap();
            List<BIZ_phieunhap> ds = dsphieunhap;
            ds = (from d in ds where d.ngaynhap.Date >= DateTime.Parse(tungay.Value.ToString()).Date && d.ngaynhap.Date <= DateTime.Parse(denngay.Value.ToString()).Date select d).ToList();
            int stt = 0;
            foreach (BIZ_phieunhap _pn in ds)
            {
                stt++;
                string tongtien = _pn.tongtien.ToString("c");
                tongtien = tongtien.Substring(1, tongtien.LastIndexOf(".") - 1);
                danhsachphieunhap.Rows.Add(stt, _pn.maphieunhap, _pn.ngaynhap.Date.ToShortDateString(),
                                            _pn.nhacungcap, _pn.diachi, _pn.sodienthoai, _pn.email, tongtien);
            }
        }

        private void btnxuatphieunhap_Click(object sender, EventArgs e)
        {
            if (danhsachchitietphieunhap.RowCount < 1)
                MessageBox.Show("Vui lòng chọn phiếu nhập", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                Report rp = new Report();
                rp.ExportToExcel_phieunhap(txtmaphieu, txtngaynhap, txtnhacungcap, txtdiachi, txtsodienthoai, txtemail, txttongtien, danhsachchitietphieunhap);
            }
        }
    }
}
